package com.tjp.eight.ui.fragment.mywallet.details;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.views.rcview.EndlessRecyclerOnScrollListener;
import com.tjp.eight.views.rcview.HeaderViewRecyclerAdapter;

/**
 * Created by Administrator on 2017/3/28.
 */
public class FragmentWalletList extends BaseFragment<WalletListPresenter> implements WalletListView {
    RecyclerView rv_wallet_list;
    HeaderViewRecyclerAdapter headerViewRecyclerAdapter;
    WalletListAdapter walletListAdapter;
    View footView;

    TextView tv_no_data;

    @Override
    protected WalletListPresenter createPresenter() {
        return new WalletListPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_wallet_list, container, false);
        }
        initViews();
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.loadData(getPJson("1"));
    }

    private String getPJson(String page) {
        TListModel model = new TListModel();
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        model.page = page;
        return new Gson().toJson(model);
    }

    private void initViews() {
        tv_no_data = (TextView) contentView.findViewById(R.id.tv_no_data);
        rv_wallet_list = (RecyclerView) contentView.findViewById(R.id.rv_wallet_list);
        rv_wallet_list.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        walletListAdapter = new WalletListAdapter(getActivity());
        headerViewRecyclerAdapter = new HeaderViewRecyclerAdapter(walletListAdapter);
        footView = LayoutInflater.from(getContext()).inflate(R.layout.swipe_refresh_footer, rv_wallet_list, false);
        rv_wallet_list.setAdapter(headerViewRecyclerAdapter);
        rv_wallet_list.addOnScrollListener(new EndlessRecyclerOnScrollListener(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false)) {
            @Override
            public void onLoadMore(int currentPage) {
                presenter.loadMData(getPJson(currentPage + ""));
            }
        });
        //headerViewRecyclerAdapter.addFooterView(footView);
    }

    @Override
    public void loadSuccess(WallistRModel model) {
        if (model != null && model.data != null) {
            walletListAdapter.addData(model.data.list);
        }
        if (walletListAdapter.getItemCount() == 0) {
            rv_wallet_list.setVisibility(View.GONE);
            tv_no_data.setVisibility(View.VISIBLE);
        } else {
            rv_wallet_list.setVisibility(View.VISIBLE);
            tv_no_data.setVisibility(View.GONE);
        }
    }

    @Override
    public void loadMSuccess(WallistRModel model) {
        if (model != null && model.data != null) {
            walletListAdapter.addMData(model.data.list);
        }
        if (walletListAdapter.getItemCount() == 0) {
            rv_wallet_list.setVisibility(View.GONE);
            tv_no_data.setVisibility(View.VISIBLE);
        } else {
            rv_wallet_list.setVisibility(View.VISIBLE);
            tv_no_data.setVisibility(View.GONE);
        }
    }
}
